<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我在阿里学到的10条Java编码铁律 - 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
            background-color: #f8fafc;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            color: #1e293b;
        }
        .hero {
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .code-toolbar {
            background-color: #0f172a;
            padding: 0.5rem 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .code-toolbar span {
            color: #94a3b8;
            font-size: 0.875rem;
        }
        pre {
            margin: 0;
            padding: 1rem;
            color: #e2e8f0;
            font-family: 'Courier New', Courier, monospace;
            font-size: 0.875rem;
            line-height: 1.5;
            overflow-x: auto;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
        .card:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .highlight {
            position: relative;
        }
        .highlight::before {
            content: "";
            position: absolute;
            left: -4px;
            top: 0;
            height: 100%;
            width: 4px;
            background-color: #3b82f6;
            border-radius: 4px 0 0 4px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0 md:pr-10">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4 leading-tight">我在阿里学到的10条Java编码铁律</h1>
                    <p class="text-xl mb-6 text-blue-100">从代码新手到架构师的成长之路</p>
                    <p class="text-lg mb-8">真正的高手代码，连空行都是艺术</p>
                    <div class="flex space-x-4">
                        <a href="#content" class="bg-white text-blue-600 px-6 py-3 rounded-lg font-semibold hover:bg-blue-50 transition-all">开始阅读</a>
                        <a href="#summary" class="border-2 border-white text-white px-6 py-3 rounded-lg font-semibold hover:bg-white hover:bg-opacity-10 transition-all">快速了解</a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <img src="https://cdn.nlark.com/yuque/0/2025/png/21449790/1743484739352-5089a9bb-cfba-46b1-8e20-5b7bf502576c.png" alt="Java编码艺术" class="rounded-lg shadow-xl">
                </div>
            </div>
        </div>
    </section>

    <!-- Content Summary -->
    <section id="summary" class="py-12 bg-white">
        <div class="container mx-auto max-w-4xl px-4">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">本文揭秘我在阿里学到的10条保命铁律</h2>
                <p class="text-gray-600 max-w-2xl mx-auto">从代码规范到架构设计，这些经验价值百万</p>
            </div>
            <div class="grid md:grid-cols-3 gap-8">
                <div class="card bg-white p-6 rounded-xl border border-gray-100">
                    <div class="text-blue-500 text-2xl mb-4">
                        <i class="fas fa-code-branch"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">代码重构艺术</h3>
                    <p class="text-gray-600">一个3000行的类如何被我们拆到300行，遵循单一职责原则</p>
                </div>
                <div class="card bg-white p-6 rounded-xl border border-gray-100">
                    <div class="text-blue-500 text-2xl mb-4">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">异常处理实战</h3>
                    <p class="text-gray-600">用异常处理避免百万级资金损失的真实案例</p>
                </div>
                <div class="card bg-white p-6 rounded-xl border border-gray-100">
                    <div class="text-blue-500 text-2xl mb-4">
                        <i class="fas fa-keyboard"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">命名艺术</h3>
                    <p class="text-gray-600">让代码自己说话的命名魔法，杜绝a1、data2这类命名</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section id="content" class="py-12 bg-gray-50">
        <div class="container mx-auto max-w-4xl px-4">
            <!-- Article 1 -->
            <article class="mb-16">
                <h2 class="text-3xl font-bold mb-6">一、你的变量名暴露了编程年限</h2>
                <p class="text-lg text-gray-700 mb-8">我见过最离谱的变量命名是<code class="bg-gray-200 px-1 py-0.5 rounded">a1</code>和<code class="bg-gray-200 px-1 py-0.5 rounded">data2</code>——这简直是在用摩斯密码写Java！</p>
                
                <div class="mb-8">
                    <h3 class="text-xl font-semibold mb-4 text-red-500">反面教材（某物流系统源码）</h3>
                    <div class="code-block">
                        <div class="code-toolbar">
                            <span>BadExample.java</span>
                            <span class="text-xs">反面教材</span>
                        </div>
                        <pre><code>public void pdx(int a, String b) { // 快递处理？怕得罪谁不敢写全称？
    List&lt;Object&gt; list = new ArrayList&lt;&gt;(); // 这个list装的是啥？
    // ... 200行后
    list.add(123); // 夭寿啦！数字往Object列表里塞
}</code></pre>
                    </div>
                </div>
                
                <div class="mb-8">
                    <h3 class="text-xl font-semibold mb-4 text-green-500">正确姿势（订单中心实战）</h3>
                    <div class="code-block">
                        <div class="code-toolbar">
                            <span>GoodExample.java</span>
                            <span class="text-xs">最佳实践</span>
                        </div>
                        <pre><code>// 物流轨迹更新
public void updateDeliveryTracking(String trackingNumber, DeliveryStatus status) {
    List&lt;DeliveryEvent&gt; deliveryEvents = new ArrayList&lt;&gt;(10);
    deliveryEvents.add(new DeliveryEvent(LocalDateTime.now(), status));
    // ...
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-blue-50 p-6 rounded-lg">
                    <h4 class="text-xl font-semibold mb-4 text-blue-600">命名三要素：</h4>
                    <ol class="list-decimal pl-6 space-y-2">
                        <li class="font-medium">见名知意（如<code class="bg-blue-100 px-1 py-0.5 rounded">calculateOrderTotal</code>代替<code class="bg-blue-100 px-1 py-0.5 rounded">calc</code>）</li>
                        <li class="font-medium">杜绝缩写（<code class="bg-blue-100 px-1 py-0.5 rounded">custAddr</code>不如<code class="bg-blue-100 px-1 py-0.5 rounded">customerAddress</code>）</li>
                        <li class="font-medium">统一术语（全系统使用<code class="bg-blue-100 px-1 py-0.5 rounded">discountRate</code>或<code class="bg-blue-100 px-1 py-0.5 rounded">rebateRatio</code>二选一）</li>
                    </ol>
                </div>
            </article>
            
            <!-- Article 2 -->
            <article class="mb-16">
                <h2 class="text-3xl font-bold mb-6">二、别让类膨胀成代码肿瘤</h2>
                <p class="text-lg text-gray-700 mb-8">某电商平台的<code class="bg-gray-200 px-1 py-0.5 rounded">OrderService</code>类曾重达3800行，直到某次大促时触发了JVM方法区OOM。我们通过<strong>领域拆分</strong>将其瘦身：</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-red-500">拆分前</h3>
                        <div class="code-block h-full">
                            <div class="code-toolbar">
                                <span>OrderService.java</span>
                                <span class="text-xs">问题代码</span>
                            </div>
                            <pre><code>class OrderService {
    void createOrder() {/* 500行 */}
    void cancelOrder() {/* 300行 */}
    void applyCoupon() {/* 200行 */} 
    // 还有15个方法...
}</code></pre>
                        </div>
                    </div>
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-green-500">拆分后</h3>
                        <div class="code-block h-full">
                            <div class="code-toolbar">
                                <span>OrderInterfaces.java</span>
                                <span class="text-xs">优化方案</span>
                            </div>
                            <pre><code>interface OrderOperator {
    void createOrder();
    void cancelOrder();
}

interface CouponApplicator {
    void applyCoupon();
    void calculateDiscount();
}

interface InventoryUpdater {
    void deductStock();
    void restoreStock();
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="bg-yellow-50 p-6 rounded-lg">
                    <h4 class="text-xl font-semibold mb-4 text-yellow-600">类设计黄金法则：</h4>
                    <ul class="list-disc pl-6 space-y-2">
                        <li class="font-medium">单一职责原则（一个类只做一件事）</li>
                        <li class="font-medium">方法长度不超过IDE屏幕高度</li>
                        <li class="font-medium">使用设计模式取代条件嵌套</li>
                    </ul>
                </div>
            </article>
            
            <!-- Article 3 -->
            <article class="mb-16">
                <h2 class="text-3xl font-bold mb-6">三、别用printStackTrace糊弄事</h2>
                <p class="text-lg text-gray-700 mb-8">某支付系统曾因异常处理不当，导致用户重复扣款：</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-red-500">灾难代码</h3>
                        <div class="code-block h-full">
                            <div class="code-toolbar">
                                <span>PaymentService.java</span>
                                <span class="text-xs">错误示例</span>
                            </div>
                            <pre><code>try {
    paymentService.charge(amount);
} catch (Exception e) {
    e.printStackTrace(); // 日志文件瞬间爆炸
    return "支付失败"; // 前端显示失败，实际已扣款
}</code></pre>
                        </div>
                    </div>
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-green-500">正确姿势（银行级处理）</h3>
                        <div class="code-block h-full">
                            <div class="code-toolbar">
                                <span>PaymentProcessor.java</span>
                                <span class="text-xs">最佳实践</span>
                            </div>
                            <pre><code>try {
    PaymentResult result = paymentClient
        .chargeWithIdempotency(orderId, amount);
    if (result.isSuccess()) {
        return renderSuccess(result.getTransactionId());
    }
    return renderFailure(PAYMENT_GATEWAY_ERROR, 
        result.getErrorMessage());
} catch (PaymentTimeoutException e) {
    log.warn("支付网关超时，订单号：{}", orderId, e);
    return renderRetrySuggestion();
} catch (InvalidAmountException e) {
    auditService.recordSuspiciousOperation(userId);
    throw new BusinessException("金额校验失败", e);
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="bg-purple-50 p-6 rounded-lg">
                    <h4 class="text-xl font-semibold mb-4 text-purple-600">异常处理三原则：</h4>
                    <ol class="list-decimal pl-6 space-y-2">
                        <li class="font-medium">永远不要吞掉异常</li>
                        <li class="font-medium">使用精确异常类型捕获</li>
                        <li class="font-medium">日志要包含上下文信息</li>
                    </ol>
                </div>
            </article>
            
            <!-- Article 4 -->
            <article class="mb-16">
                <h2 class="text-3xl font-bold mb-6">四、好代码自己会说话</h2>
                <p class="text-lg text-gray-700 mb-8">某著名开源项目的反面教材：</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-red-500">反面教材</h3>
                        <div class="code-block h-full">
                            <div class="code-toolbar">
                                <span>PriceCalculator.java</span>
                                <span class="text-xs">冗余代码</span>
                            </div>
                            <pre><code>// 这个方法用来计算价格
public double calcPrice() {
    // 获取基础价格
    double price = getBasePrice();
    // 计算折扣
    price = price * discount;
    // 返回结果
    return price;
}</code></pre>
                        </div>
                    </div>
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-green-500">改造后的自解释代码</h3>
                        <div class="code-block h-full">
                            <div class="code-toolbar">
                                <span>PriceEngine.java</span>
                                <span class="text-xs">自解释代码</span>
                            </div>
                            <pre><code>public double calculateDiscountedPrice() {
    final double basePrice = pricingStrategy
        .getBasePrice(productId);
    return applyDiscountPolicy(basePrice);
}

private double applyDiscountPolicy(double basePrice) {
    return discountCalculator
        .calculate(basePrice, userLevel);
}</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="bg-green-50 p-6 rounded-lg">
                    <h4 class="text-xl font-semibold mb-4 text-green-600">注释使用准则：</h4>
                    <ul class="list-disc pl-6 space-y-2">
                        <li class="font-medium">方法注释用JavaDoc（可被IDE识别）</li>
                        <li class="font-medium">废弃代码用<code class="bg-green-100 px-1 py-0.5 rounded">@Deprecated</code>注解</li>
                        <li class="font-medium">复杂算法配流程图</li>
                        <li class="font-medium">禁止出现"这里修改过"等无意义注释</li>
                    </ul>
                </div>
            </article>
            
            <!-- Article 5 -->
            <article class="mb-16">
                <h2 class="text-3xl font-bold mb-6">五、你的代码真的能跑吗？</h2>
                <p class="text-lg text-gray-700 mb-8">某社交平台的血泪教训：用户关注功能在代码合并后导致粉丝数错乱，只因缺少单元测试。</p>
                
                <div class="mb-8">
                    <h3 class="text-xl font-semibold mb-4 text-blue-500">JUnit 5最佳实践</h3>
                    <div class="code-block">
                        <div class="code-toolbar">
                            <span>FollowServiceTest.java</span>
                            <span class="text-xs">单元测试示例</span>
                        </div>
                        <pre><code>@DisplayName("用户关注场景测试")
class FollowServiceTest {

    @Mock UserRepository userRepo;
    @Mock FollowRelationRepository followRepo;
    
    @InjectMocks FollowService followService;

    @Test
    @DisplayName("当用户首次关注时，应创建关系记录")
    void shouldCreateFollowRelationWhenFirstTime() {
        // Given
        when(userRepo.existsById(1L)).thenReturn(true);
        when(followRepo.existsByFromUserIdAndToUserId(1L, 2L))
            .thenReturn(false);

        // When
        FollowResult result = followService.follow(1L, 2L);

        // Then
        assertThat(result).isEqualTo(FollowResult.SUCCESS);
        verify(followRepo).save(any(FollowRelation.class));
    }

    @Test
    @DisplayName("当重复关注时，应返回重复操作错误")
    void shouldReturnDuplicateErrorWhenFollowAgain() {
        // ...类似结构
    }
}</code></pre>
                    </div>
                </div>
                
                <div class="bg-indigo-50 p-6 rounded-lg">
                    <h4 class="text-xl font-semibold mb-4 text-indigo-600">测试覆盖率目标：</h4>
                    <div class="grid md:grid-cols-3 gap-4">
                        <div class="bg-white p-4 rounded-lg text-center">
                            <div class="text-indigo-500 text-2xl mb-2">
                                <i class="fas fa-bullseye"></i>
                            </div>
                            <h5 class="font-semibold">核心业务逻辑</h5>
                            <p class="text-indigo-600 font-bold">100%</p>
                        </div>
                        <div class="bg-white p-4 rounded-lg text-center">
                            <div class="text-indigo-500 text-2xl mb-2">
                                <i class="fas fa-tools"></i>
                            </div>
                            <h5 class="font-semibold">工具类</h5>
                            <p class="text-indigo-600 font-bold">90%+</p>
                        </div>
                        <div class="bg-white p-4 rounded-lg text-center">
                            <div class="text-indigo-500 text-2xl mb-2">
                                <i class="fas fa-plug"></i>
                            </div>
                            <h5 class="font-semibold">第三方集成</h5>
                            <p class="text-indigo-600 font-bold">Mock测试</p>
                        </div>
                    </div>
                </div>
            </article>
            
            <!-- Article 6 -->
            <article class="mb-16">
                <h2 class="text-3xl font-bold mb-6">六、别用肉眼Review代码</h2>
                <p class="text-lg text-gray-700 mb-8">我团队的IDEA必须安装这些插件：</p>
                
                <div class="grid md:grid-cols-3 gap-6 mb-8">
                    <div class="card bg-white p-6 rounded-xl border border-gray-100">
                        <div class="text-red-500 text-2xl mb-4">
                            <i class="fas fa-check-circle"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">Checkstyle</h3>
                        <p class="text-gray-600">代码规范检查，强制统一编码风格</p>
                    </div>
                    <div class="card bg-white p-6 rounded-xl border border-gray-100">
                        <div class="text-blue-500 text-2xl mb-4">
                            <i class="fas fa-bug"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">SonarLint</h3>
                        <p class="text-gray-600">代码异味检测，发现潜在问题</p>
                    </div>
                    <div class="card bg-white p-6 rounded-xl border border-gray-100">
                        <div class="text-green-500 text-2xl mb-4">
                            <i class="fas fa-search"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3">SpotBugs</h3>
                        <p class="text-gray-600">潜在BUG扫描，静态分析工具</p>
                    </div>
                </div>
                
                <div class="mb-8">
                    <h3 class="text-xl font-semibold mb-4">Maven配置示例</h3>
                    <div class="code-block">
                        <div class="code-toolbar">
                            <span>pom.xml</span>
                            <span class="text-xs">构建配置</span>
                        </div>
                        <pre><code>&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-checkstyle-plugin&lt;/artifactId&gt;
    &lt;version&gt;3.2.0&lt;/version&gt;
    &lt;configuration&gt;
        &lt;configLocation&gt;google_checks.xml&lt;/configLocation&gt;
        &lt;violationSeverity&gt;warning&lt;/violationSeverity&gt;
    &lt;/configuration&gt;
    &lt;executions&gt;
        &lt;execution&gt;
            &lt;phase&gt;validate&lt;/phase&gt;
            &lt;goals&gt;
                &lt;goal&gt;check&lt;/goal&gt;
            &lt;/goals&gt;
        &lt;/execution&gt;
    &lt;/executions&gt;
&lt;/plugin&gt;</code></pre>
                    </div>
                </div>
                
                <div class="bg-pink-50 p-6 rounded-lg">
                    <h4 class="text-xl font-semibold mb-4 text-pink-600">代码规范的三个境界</h4>
                    <div class="space-y-4">
                        <div class="flex items-start">
                            <div class="bg-pink-100 text-pink-600 rounded-full w-8 h-8 flex items-center justify-center mr-4 flex-shrink-0">1</div>
                            <div>
                                <h5 class="font-semibold">新手期</h5>
                                <p class="text-gray-600">靠IDE插件被动约束</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-pink-100 text-pink-600 rounded-full w-8 h-8 flex items-center justify-center mr-4 flex-shrink-0">2</div>
                            <div>
                                <h5 class="font-semibold">成长期</h5>
                                <p class="text-gray-600">形成肌肉记忆</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="bg-pink-100 text-pink-600 rounded-full w-8 h-8 flex items-center justify-center mr-4 flex-shrink-0">3</div>
                            <div>
                                <h5 class="font-semibold">高手期</h5>
                                <p class="text-gray-600">规范内化为设计直觉</p>
                            </div>
                        </div>
                    </div>
                </div>
            </article>
            
            <!-- Visualization -->
            <article class="mb-16">
                <h2 class="text-3xl font-bold mb-6">Java编码规范演进之路</h2>
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="mermaid">
                        graph TD
                            A[新手程序员] -->|学习基础| B[使用IDE插件]
                            B -->|形成习惯| C[主动遵守规范]
                            C -->|经验积累| D[规范内化为直觉]
                            D --> E[优雅的代码艺术家]
                            
                            style A fill:#f8fafc,stroke:#334155
                            style B fill:#e0f2fe,stroke:#0369a1
                            style C fill:#bae6fd,stroke:#0ea5e9
                            style D fill:#7dd3fc,stroke#38bdf8
                            style E fill:#38bdf8,stroke#0284c7,color:#fff
                    </div>
                </div>
            </article>
        </div>
    </section>
    
    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-4xl px-4 text-center">
            <h3 class="text-xl font-semibold mb-2 text-white">技术小馆</h3>
            <p class="mb-6">分享有价值的编程知识与实践经验</p>
            <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors">语雀地址: www.yuque.com/jtostring</a>
            <div class="mt-8 pt-8 border-t border-gray-800">
                <p class="text-sm text-gray-500">© 2023 技术小馆. 版权所有.</p>
            </div>
        </div>
    </footer>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            mermaid.initialize({
                theme: 'default',
                startOnLoad: true,
                flowchart: {
                    useMaxWidth: false,
                    htmlLabels: true,
                    curve: 'basis'
                }
            });
            
            // 平滑滚动
            document.querySelectorAll('a[href^="#"]').forEach(anchor => {
                anchor.addEventListener('click', function (e) {
                    e.preventDefault();
                    document.querySelector(this.getAttribute('href')).scrollIntoView({
                        behavior: 'smooth'
                    });
                });
            });
        });
    </script>
</body>
</html>